using System;
using System.Collections;
using System.IO;
using System.Security;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using FormLabel = CommunityServer.ControlPanel.Controls.FormLabel;
using ResourceManager = FourRoads.Links.Controls.ResourceManager;
using HelpIcon = CommunityServer.ControlPanel.Controls.HelpIcon;

namespace FourRoads.Links.Controls
{
	/// <summary>
	/// Summary description for GeneralSettings.
	/// </summary>
	public class GeneralSettings : ResLinkAdminPage
	{
		protected YesNoRadioButtonList ynHideDisabledSections;
		protected ResourceLinkButton Save;
		protected CommunityServer.ControlPanel.Controls.StatusMessage Status;
		protected YesNoRadioButtonList ynDisplaySectionCounts;
		protected TextBox AdCount;
		protected YesNoRadioButtonList ynDisplayLinkCounts;
		protected YesNoRadioButtonList ynAnonymousRatings;
		protected YesNoRadioButtonList ynPromoteLinksIntoParentSections;


		private ResLinkConfiguration _Config = null;

		public GeneralSettings()
		{
			_Config = ResLinkConfiguration.Instance();
		}

		override protected void OnInit(EventArgs e)
		{
			this.Load += new EventHandler(this.Page_Load);
			Save.Click += new EventHandler(Save_Click);
			base.OnInit(e);
		}
		
		private void Page_Load(object sender, EventArgs e)
		{
			CSContext context = CSContext.Current;
			Status.Visible = false;
			Status.Text = ResourceManager.GetString("CP_ResLink_SaveSuccessful");

			if(!Page.IsPostBack)
			{
				BindSettingsData();
			}
		}

		private void BindSettingsData()
		{
			ynHideDisabledSections.SelectedValue = _Config.HideDisabledSections;
			ynDisplaySectionCounts.SelectedValue = _Config.DisplaySectionCounts;
			AdCount.Text = _Config.AdvertRepeatFrequency.ToString();
			ynDisplayLinkCounts.SelectedValue = _Config.DisplayLinkCounts;
			ynAnonymousRatings.SelectedValue = _Config.AnonymousRatings;
			ynPromoteLinksIntoParentSections.SelectedValue = _Config.PromoteLinksIntoParentSections;
		}

		private void Save_Click(object sender, EventArgs e)
		{
			//Validate we are an Admin
			if(!ExtendedUserRoles.IsLinkAdministrator)
				throw new CSException(CSExceptionType.AdministrationAccessDenied);

			_Config.HideDisabledSections = ynHideDisabledSections.SelectedValue;
			_Config.DisplaySectionCounts = ynDisplaySectionCounts.SelectedValue;
			_Config.AdvertRepeatFrequency = System.Convert.ToInt32(AdCount.Text );
			_Config.DisplayLinkCounts = ynDisplayLinkCounts.SelectedValue;
			_Config.AnonymousRatings = ynAnonymousRatings.SelectedValue;
			_Config.PromoteLinksIntoParentSections = ynPromoteLinksIntoParentSections.SelectedValue;

			Status.Visible = true;
			_Config.Save();

			//Dramtica changes require the cache to be cleared completley
			ResLinkSection.ClearAllSectionCache();
		}
	}
}
